package com.lcz.googleplay.holder;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import com.lcz.googleplay.R;
import com.lcz.googleplay.config.BaseProtocol;
import com.lcz.googleplay.domain.DetailInfo;
import com.lcz.googleplay.utils.UIUtils;
import com.yolanda.nohttp.rest.Response;
import com.yolanda.nohttp.rest.SimpleResponseListener;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hp on 2016/11/9.
 */

public class Detail_ScreenHolder extends BaseHolder<DetailInfo> {
    @BindView(R.id.screen_1)
    ImageView screen1;
    @BindView(R.id.screen_2)
    ImageView screen2;
    @BindView(R.id.screen_3)
    ImageView screen3;
    @BindView(R.id.screen_4)
    ImageView screen4;
    @BindView(R.id.screen_5)
    ImageView screen5;
    private ImageView[] imags;


    @Override
    public View init() {
        imags = new ImageView[5];
        View view = View.inflate(UIUtils.getActivity(), R.layout.detail_screen, null);
        ButterKnife.bind(this, view);
        imags[0] = screen1;
        imags[1] = screen2;
        imags[2] = screen3;
        imags[3] = screen4;
        imags[4] = screen5;
        return view;
    }

    @Override
    public void refreshView(DetailInfo data) {

        for (int i = 0; i < 5; i++) {
            if (i < data.getScreen().size()) {
                imags[i].setVisibility(View.VISIBLE);
                //需要开启服务器获取图片
                String imageUri = BaseProtocol.IMAGE_URL + data.getScreen().get(i);
                final int finalI = i;
                SimpleResponseListener<Bitmap> listener = new SimpleResponseListener<Bitmap>() {
                    @Override
                    public void onSucceed(int what, Response<Bitmap> response) {
                        super.onSucceed(what, response);
                        imags[finalI].setImageBitmap(response.get());
                    }

                    @Override
                    public void onFailed(int what, Response<Bitmap> response) {
                        super.onFailed(what, response);
                        imags[finalI].setImageResource(R.drawable.ic_default);
                    }
                };
                BaseProtocol.loadImage(imageUri, listener);
            }else {
                imags[i].setVisibility(View.GONE);
            }
        }

    }
}
